package com.youlu.campus.admin.linker;

import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.ShortUrlConfig;
import com.youlu.campus.entity.ShortUrlConfigStatistics;
import com.youlu.campus.entity.VO.req.ShortUrlConfigQueryVO;
import com.youlu.campus.entity.VO.req.ShortUrlConfigStatisticsQueryVO;
import com.youlu.campus.service.linker.ShortUrlConfigService;
import com.youlu.campus.service.linker.ShortUrlConfigStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ShortUrlConfigController {
    @Autowired
    private ShortUrlConfigService shortUrlConfigService;
    @Autowired
    private ShortUrlConfigStatisticsService shortUrlConfigStatisticsService;

    @PostMapping("/short-url/list")
    public RestResult list(@RequestBody @Valid ShortUrlConfigQueryVO req) {
        String comId = AuthUtils.getComId();
        String loginName = AuthUtils.getLoginName();
        if (!"zhuhuaiqi".equals(loginName) && !"zhouqiao".equals(loginName) && !"sujiakang".equals(loginName)) {
            req.setOptLoginName(loginName);
        }
        Page<ShortUrlConfig> datas = shortUrlConfigService.list(req);
        if (datas == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            datas = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(datas).build();
    }

    @PostMapping("/short-url/create")
    public RestResult create(@RequestBody @Valid ShortUrlConfig req) {
        String comId = AuthUtils.getComId();
        req.setOptLoginName(AuthUtils.getLoginName());
        boolean re = shortUrlConfigService.create(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/short-url/update")
    public RestResult list(@RequestBody @Valid ShortUrlConfig req) {
        String comId = AuthUtils.getComId();
        req.setOptLoginName(AuthUtils.getLoginName());
        boolean re = shortUrlConfigService.update(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/short-url/find-one")
    public RestResult findOne(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        ShortUrlConfig re = shortUrlConfigService.findOne(id);
        return RestResultBuilder.builder().success().data(re).build();
    }

    @GetMapping("/short-url/delete")
    public RestResult delete(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        boolean re = shortUrlConfigService.delete(id);
        return RestResultBuilder.builder().success().build();
    }

    @PostMapping("/short-url/statistics")
    public RestResult statistics(@RequestBody @Valid ShortUrlConfigStatisticsQueryVO req) {
        String comId = AuthUtils.getComId();
        List<ShortUrlConfigStatistics> data = shortUrlConfigStatisticsService.list(req);
        return RestResultBuilder.builder().success(data).build();
    }
}
